package com.augurit.common.system.web.org;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.IconMultiSelect;
import com.augurit.common.system.convert.AcUserShortcutConverter;
import com.augurit.common.system.service.org.IAcMenuService;
import com.augurit.common.system.service.org.IAcUserShortcutService;
import com.augurit.common.system.web.org.form.AcUserShortcutForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.ArrayUtils;


@SuppressWarnings("serial")
@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "ac-user-shortcut.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/common/system/org/ac-user-shortcut.jsp"),
	@Result(name = AcUserShortcutAction.ADMINUSER, location = "/common/system/org/ac-admin-shortcut.jsp")
})
public class AcUserShortcutAction extends ExtCrudActionSupport<AcUserShortcutForm> {
	
	public static final String ADMINUSER = "adminUser";
	
	// Service 引用
	@Autowired
	private IAcUserShortcutService acUserShortcutService;
	@Autowired
	private IAcMenuService acMenuService;
	
	// 基本属性
	private AcUserShortcutForm form;
	private Long id;
	private Page<AcUserShortcutForm> page = new Page<AcUserShortcutForm>();
	
	private Long userId;			//用户id，对一个用户操作
	private Long[] userIds;			//用户id数组，对多用户的操作
	private Long[] newShortcutIds;	//快捷菜单id数组
	private String insertPos;		//插入位置
	private Long menuId;			//菜单id
	

	public String admin() throws Exception {
		return ADMINUSER;
	}
	/**
	 * 快捷菜单列表查询
	 */
	@Override
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		acUserShortcutService.getShortcuts(page);
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 清空指定用户的已有的所有快捷菜单，重新添加新的快捷菜单
	 * @param userId 用户id 
	 * @param newShortcutIds 给用户添加的快捷菜单id数组
	 */
	public String updateAll() throws Exception{
		if(userId != null && newShortcutIds != null && newShortcutIds.length > 0){
			//清空用户的所有快捷菜单
			acUserShortcutService.clearShortcut(userId);
			//插入快捷菜单
			acUserShortcutService.insertAtFirst(userId, newShortcutIds);
			this.extRenderSuccess();
		}
		return null;
	}
	
	
	/**
	 * 清空当前登录用户的已有的所有快捷菜单，重新添加新的快捷菜单
	 * @param newShortcutIds 给用户添加的快捷菜单id数组
	 */
	public String updateAllForCurUser() throws Exception{
		//获取当前登录用户ID
		Long userId = this.getLoginUserId();
		//清空用户的所有快捷菜单
		acUserShortcutService.clearShortcut(userId);
		//插入快捷菜单
		if(newShortcutIds != null && newShortcutIds.length > 0){
			acUserShortcutService.insertAtFirst(userId, newShortcutIds);
			this.extRenderSuccess();
		}
		return null;
	}
	
	/**
	 * 删除某一个用户的指定的一组快捷菜单
	 * @param userId 用户id
	 * @param newShortcutIds 给用户添加的快捷菜单id数组
	 */
	public String deleteShortcutFromUser() throws Exception{
		if(userId != null && newShortcutIds != null && newShortcutIds.length > 0){
			acUserShortcutService.deleteShortcutFromUser(userId, newShortcutIds);
			this.extRenderSuccess();
		}
		return null;
	}
	
	/**
	 * 删除使用某一个快捷菜单的一组用户
	 * @param menuId 菜单id
	 * @param userId 用户id数组
	 */
	public String deleteUsersFromShortcut() throws Exception{
		if(menuId != null && userIds != null && userIds.length > 0){
			acUserShortcutService.deleteUsersFromShortcut(menuId, userIds);
			this.extRenderSuccess();
		}
		return null;
	}
	
	/**
	 * 对于某一个快捷菜单，添加使用该快捷菜单的用户
	 * @param userIds 用户id数组
	 * @param menuId 菜单id
	 */
	public String addUsersUsingShortcut() throws Exception{
		if(userIds != null && userIds.length > 0){
			for(Long userId:userIds){
				acUserShortcutService.insertAtLast(userId, menuId);
			}
			this.extRenderSuccess();
		}
		return null;
	}
	
	/**
	 * 计算需要为用户添加的快捷菜单id数组
	 *   公式:  需要为用户添加的快捷菜单  = 页面上被选择的菜单 - 用户不具有权限的被选择的菜单 - 已经成为该用户的快捷菜单的菜单
	 * @param userId 用户id
	 * @param shortcutsBeSelected 页面上被选择的菜单id
	 * @return 需要为用户添加的快捷菜单id数组
	 */
	private Long[] calShortcutsNeedToAdd(Long userId, Long[] shortcutsBeSelected){
		if(userId != null && shortcutsBeSelected != null && shortcutsBeSelected.length > 0){
			//获取用户的具有权限的所有菜单
			Long[] menuIdsOwnedByUser = acMenuService.getMenuIdsOwnedByUser(userId);
			//排除用户不具有权限的快捷菜单
			Long[] newShortcuts = ArrayUtils.calIntersection(shortcutsBeSelected, menuIdsOwnedByUser);
			//获取用户已有的快捷菜单
			Long[] shortcutsOwnedByUser = acUserShortcutService.getShortcutIdsByUserId(userId);
			//排除已经有的快捷菜单
			newShortcuts = ArrayUtils.calDifference(newShortcuts, shortcutsOwnedByUser);
			
			return newShortcuts;
		}
		return null;
	}
	
	/**
	 * 为多个用户添加多个快捷菜单
	 * @param userIds 用户id数组
	 * @param newShortcutIds 被选择成为快捷菜单的菜单id数组
	 */
	public String addShortcutToUsers() throws Exception{
		 
		if(userIds != null && userIds.length > 0){
			for(Long userId:userIds){
				Long[] newShortcuts = calShortcutsNeedToAdd(userId, newShortcutIds);
				
				//插入在所有快捷菜单的最前面
				if(insertPos.equals(AcUserShortcutForm.INSERT_AT_FIRST)){
					acUserShortcutService.insertAtFirst(userId, newShortcuts);
				}
				//插入在所有快捷菜单的最后面
				else if(insertPos.equals(AcUserShortcutForm.INSERT_AT_LAST)){
					acUserShortcutService.insertAtLast(userId, newShortcuts);
				}
				//插入在中间某个位置
				else{
					int pos = Integer.parseInt(insertPos.split("=")[1]);
					acUserShortcutService.insertAtMiddle(userId, pos, newShortcuts);
				}
			}
		}
		return null;
	}
	
	/**
	 * 获取指定用户的快捷菜单
	 * @param userId 用户id
	 * @param form 保存查询菜单的参数
	 */
	public String getShortcutsOwnedByUser() throws Exception {
		//获取 Ext Grid 的分页信息
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		if( userId != null){
			acUserShortcutService.getShortcuts(page, form, userId);
			this.extRenderGridJson(page);
			return null;
		}
		return null;
	}
	
	/**
	 * 获取当前登录的用户的快捷菜单
	 */
	public String getShortcutOfCurUser() throws Exception{
		List<AcUserShortcutForm> list = acUserShortcutService.getShortcuts(this.getLoginUserId());
		List<IconMultiSelect> comboList = AcUserShortcutConverter.convertToBasicCombo(list, false);
		this.extRenderListJson(comboList, true);
		return null;
	}
	
	
	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public AcUserShortcutForm getModel() {
		return form;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if( id != null ){
			form = acUserShortcutService.get(id);
		}else{
			form = new AcUserShortcutForm();
		}
	}
	
	
	// 属性getter和setter方法
	public AcUserShortcutForm getForm() {
		return form;
	}

	public void setForm(AcUserShortcutForm form) {
		this.form = form;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	
	public Long[] getNewShortcutIds() {
		return newShortcutIds;
	}
	public void setNewShortcutIds(Long[] newShortcutIds) {
		this.newShortcutIds = newShortcutIds;
	}
	
	public Long[] getUserIds() {
		return userIds;
	}
	public void setUserIds(Long[] userIds) {
		this.userIds = userIds;
	}
	
	public String getInsertPos() {
		return insertPos;
	}

	public void setInsertPos(String insertPos) {
		this.insertPos = insertPos;
	}
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	public Long getMenuId() {
		return menuId;
	}
	public void setMenuId(Long menuId) {
		this.menuId = menuId;
	}
}
